package be.groept.ace.enquete.model.beans;

import java.io.Serializable;
import java.util.Date;

public class UserSurvey implements Comparable<UserSurvey>, Serializable {
	private static final long serialVersionUID = 1L;
	private int id;
	private Date submitdate;
	
	private Survey survey;
	private User user;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

	public Date getSubmitdate() {
		return submitdate;
	}
	public void setSubmitdate(Date submitdate) {
		this.submitdate = submitdate;
	}
	
	public Survey getSurvey() {
		return survey;
	}
	public void setSurvey(Survey survey) throws IllegalArgumentException {
		//can only be called once -> domain logic: UserSurvey cannot be updated
		if(this.survey != null) {
			throw new IllegalArgumentException("UserSurvey cannot be updated, survey is already set");
		}
		this.survey = survey;
		//survey.addUserSurvey(this);
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) throws IllegalArgumentException {
		//can only be called once -> domain logic: UserSurvey cannot be updated
		if(this.user != null) {
			throw new IllegalArgumentException("UserSurvey cannot be updated, user is already set");
		}
		//set new user
		this.user = user;
		//user.addUserSurvey(this);
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj == null) return false;
		if(getClass() != obj.getClass()) {
			return false;
		}
		UserSurvey other = (UserSurvey)obj;
		
		return getUser().equals(other.getUser()) && getSurvey().equals(other.getSurvey());
			
	}
	
	@Override
	public int hashCode() {
		return getUser().hashCode() * 31  + getSurvey().hashCode();
	}
	
	@Override
	public int compareTo(UserSurvey o) {
		int compare = getUser().compareTo(o.getUser());
		if (compare == 0) compare = getSurvey().compareTo(o.getSurvey());
		return compare;
	}
	
	@Override
	public String toString() {
		return String.format("UserSurvey object - id: %1$d - user: %2$s - survey: %3$s - created date: %4$c", getId(), getUser(), getSurvey(), getSubmitdate());
		
	}
	
}
